package com.work.mdass.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.work.mdass.domain.TeachingMaterial;
import com.work.mdass.properties.TeachingMaterialInterface;

@Service
public class TeachingMaterialService {

	@Autowired
	private TeachingMaterialInterface teachingMaterialInterface;
	
	/*
	 * 添加一本教材
	 */
	public TeachingMaterial addMaterial(TeachingMaterial teachingMaterial) {
		
			return teachingMaterialInterface.save(teachingMaterial);
	}
	/*
	 * 获得所有教材信息
	 */
	public Map<Object,Object> getALL(){
		
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findAll();
		
		Map<Object,Object> result  =new HashMap<>();
		
		if(teachs.size()!=0) {
			result.put("code", 0);
			result.put("msg", "success");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}else {
			result.put("code", 1);
			result.put("msg", "error");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}
		return result;
	}
	/*
	 * 删除一本教材
	 */
	public Map<Object,Object> deleteMaterial(Integer id){
		

			teachingMaterialInterface.delete(id);
			
			Map<Object,Object> result  =new HashMap<>();
			
			result.put("code", 0);
			result.put("msg", "success");
			result.put("data", null);
			result.put("count", 0);
			
			return result;
	}
	/*
	 * 更新教材信息
	 */
	public TeachingMaterial updataMaterial(TeachingMaterial teachingMaterial) {
		
		return teachingMaterialInterface.save(teachingMaterial);
	}

	/*
	 * 通过教材名称查找教材
	 */
	public Map<Object,Object> getByName(String name) {
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findByName(name);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(teachs.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的数据");
			result.put("data", null);
			result.put("count", 0);
		}
		return result;
	}
	
	/*
	 * 通过教师名称查找教材
	 */
	public Map<Object,Object> getByTName(String tname) {
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findByTName(tname);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(teachs.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的数据");
			result.put("data", null);
			result.put("count", 0);
		}
		return result;
	}
	/*
	 * 通过课程编号获得教材
	 */
	public Map<Object,Object> getByCourseSerial(String courseSerial){
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findByCourseSerial(courseSerial);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(teachs.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的数据");
			result.put("data", null);
			result.put("count", 0);
		}
		return result;
		
	}
	/*
	 * 通过关键字匹配教材名称模糊搜索
	 * @return {name}
	 */
	public Map<Object,Object> getByKeyWord(String keyWord){
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findByNameLike(keyWord);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(teachs.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的数据");
			result.put("data", null);
			result.put("count", 0);
		}
		return result;
	}
	/*
	 * 通过关键字匹配教材名称模糊搜索,教师名称
	 * @return {name,tName}
	 */
	public Map<Object,Object> forUserAnalysis(String tName,String keyWord){
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findByTNameAndNameLike(tName, keyWord);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(teachs.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", teachs);
			result.put("count", teachs.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的数据");
			result.put("data", null);
			result.put("count", 0);
		}
		return result;
	}
	
	
/*	2018.3.18
	 * 判断是否已存在
	 
	public boolean isExit(Integer materialSerial){
		
		if(teachingMaterialInterface.findByMaterialSerial(materialSerial)!=null) return true;
		
		else return false;
	}*/
}
